forum

home / developersection / forums / email not being sent from mvc 3 with c# using gmail a/c

Email not being sent from MVC 3 with C# using gmail a/c

Jayden Bell 3283 02-Mar-2013

Hi Expert!

The email is not being sent from C# code using the gmail smtp settings. It is giving an error of "Server requires a Secure connection or the Client was not authenticated."

My code

MailMessage mail = new MailMessage("<from>","<to>");

            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.Credentials = new NetworkCredential("<email>", "<password>");
            client.EnableSsl = true;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Timeout = 20000;
            client.UseDefaultCredentials = false;

            mail.Body = "Test email from C# Code";
            mail.Subject = "Test Email";

            Console.WriteLine("Attempting to Send Email");

            try {
                client.Send(mail);
                Console.WriteLine("Email sent... ");
            }
            catch (System.Net.Mail.SmtpFailedRecipientException ex) {
                Console.WriteLine("Could not send email to the mentioned recipient" + ex.Message);
            }
            catch (System.Net.Mail.SmtpException ex) {
                Console.WriteLine("Could not send Email..\n" + ex.Message + "\n" + ex.StackTrace);               
            }
            Console.ReadLine();

Thanks in advance


Updated on 02-Mar-2013
Can you answer this question?

Answer

1 Answers

Liked By